package com.cy.pj.sys.service.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**通过此案例演示aop中各种通知类型的执行时间点
 * 实际项目中并不会将所有通知方法都写到一个切面类中
 * */
@Aspect
@Component
public class SysTimeAspect {
    @Pointcut("bean(sysUserServiceImpl)")
    public void doTime() {
    }

    @Before("doTime()")
    public void doBefore() {
        System.out.println("@Before");//业务执行前
    }

    @After("doTime()")
    public void doAfter() {
        System.out.println("@After");//业务结束时执行
    }

    @AfterReturning("doTime()")
    public void doAfterReturning() {
        System.out.println("@AfterReturning");
    }

    @AfterThrowing("doTime()")
    public void doAfterThrowing() {
        System.out.println("@AfterThrowing");
    }

    @Around("doTime()")
    public Object doAround(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("@Around.before");
        try {
            Object obj = jp.proceed();//执行目标方法
            System.out.println("@Around.afterReturning");
            return obj;
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("@Around.afterThrowing");
            throw e;
        }finally{
            System.out.println("@Around.After...");
        }
    }
}